/** 废弃 **/
$(function() {
    //主流程对象
    var mainCtrl = avalon.define({
            $id: "mainController",
            comm: "",
            mainCommCode: "",
            branchCode: "",
            recommList: [], //推荐商品列表
            commType: 0,
            clusterId: "",
            search: function() {
                if (mainCtrl.comm == "") {
                    return;
                }
                mainCtrl.commType = 1; //搜索咯
                if (clusterTable == null) {
                    var soUrl = DI.mobileSearch;
                    opts = {
                        targetId: "exptTable",
                        templateId: "tableSaleExpt",
                        settings: {
                            url: soUrl,
                            ajaxType: "jsonp",
                            sourceInterface: {
                                INum: "goodsCount",
                                IData: "goods"
                            },
                            paginationIndexName: {
                                "peerpageRows": "ps",
                                "currentPage": "cp"
                            },
                            searchParam: {
                                "keyword": mainCtrl.comm,
                                "channelId": "MOBILE",
                                "st": 0,
                                "ct": -1,
                                "ps": 10,
                                "cp": 0,
                                "yuyue": -1,
                                "set": 5,
                                "cityId": 9173,
                                "iv": -1,
                                "unsale": 1
                            }
                        },
                        customFuncs: {
                            getUrl: function(commcode) {
                                return mainCtrl.getUrl(commcode);
                            },
                            addComm: function(commcode) {
                                var _self = $(this);
                                if (_self.hasClass("added")) {
                                    return;
                                }
                                _self.html("").next(".add-comm").show();
                                $.ajax({
                                    type: "post",
                                    dataType: "json",
                                    data: {
                                        "mainCommCode": mainCtrl.mainCommCode,
                                        "branchCode": mainCtrl.branchCode,
                                        "commCode": commcode
                                    },
                                    url: DI.saveCluster,
                                    success: function(data) {
                                        _self.next(".add-comm").hide();
                                        if (data.retFlag == 1) {
                                            _self.addClass("added").html("已添加");
                                            if(data.data && data.data != ""){
                                                mainCtrl.clusterId = data.data;
                                            }
                                        } else {
                                            _self.removeClass("added").html("添加");
                                            PLATFORM.showTips("error", data.errorMessage);
                                        }
                                    },
                                    error: function(err) {
                                        console.log(err)
                                    }
                                });
                            }
                        }

                    };

                    clusterTable = SwiftUI.swiftTable.getInstance(opts);
                } else {
                    var searchParam = {
                        "keyword": mainCtrl.comm,
                        "channelId": "MOBILE",
                        "st": 0,
                        "ct": -1,
                        "ps": 10,
                        "cp": 0,
                        "yuyue": -1,
                        "set": 5,
                        "cityId": 9173,
                        "iv": -1,
                        "unsale": 1
                    }
                    clusterTable.search(searchParam);
                }

            },
            getUrl: function(commcode) {
                var codeLen = commcode.length;
                if (codeLen == 18) {
                    return DI.imageSplitURL + commcode + "_1_100x100.jpg";
                } else {
                    var curLen = 18 - codeLen,
                        curZero = "";
                    for (var i = 0; i < curLen; i++) {
                        curZero += "0";
                    }
                    return DI.imageSplitURL + curZero + "" + commcode + "_1_100x100.jpg";
                }
                return "";
            },
            addComm: function(commcode) {
                var _self = $(this);
                if (_self.hasClass("added")) {
                    return;
                }
                _self.html("").next(".add-comm").show();
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: {
                        "mainCommCode": mainCtrl.mainCommCode,
                        "branchCode": mainCtrl.branchCode,
                        "commCode": commcode
                    },
                    url: DI.saveCluster,
                    success: function(data) {
                        _self.next(".add-comm").hide();
                        if (data.retFlag == 1) {
                            _self.addClass("added").html("已添加");
                            if(data.data && data.data != ""){
                                mainCtrl.clusterId = data.data;
                            }
                        } else {
                            _self.removeClass("added").html("添加");
                            PLATFORM.showTips("error", data.errorMessage);
                        }
                    },
                    error: function(err) {
                        console.log(err)
                    }
                });
            },
            returnPage: function(){
                var bcode = mainCtrl.branchCode,
                    commInfo = urlParams.commInfo,
                    rcode = urlParams.regionCode,
                    commCode = mainCtrl.mainCommCode,
                    cluster = urlParams.isCluster?urlParams.isCluster:0;
                var toPage =  "clusterMaintain.html?branchCode="+bcode+"&commInfo="+commInfo+"&regionCode="+rcode+"&isCluster="+cluster+"&commCode="+commCode;
                if(mainCtrl.clusterId != ""){
                    toPage += "&clusterId=" + mainCtrl.clusterId;
                }
                window.location = toPage;
            }
        })
        //推荐商品 和 搜索商品是两个接口
    var clusterTable = null;
    var opts = {};
    //获取URL数据
    var urlParams = PLATFORM.getUrlArgs();
    mainCtrl.branchCode = urlParams.branchCode;
    mainCtrl.mainCommCode = urlParams.commCode;
    mainCtrl.clusterId = urlParams.clusterId?urlParams.clusterId:"";
    //获取推荐商品列表
    $.ajax({
        type: "post",
        dataType: "json",
        data: {
            "commCode": mainCtrl.mainCommCode,
            "branchCode": mainCtrl.branchCode
        },
        url: DI.queryRecommComm,
        success: function(data) {
            mainCtrl.recommList = data.data;
        },
        error: function(err) {
            console.log(err)
        }
    });
});